﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class ExamController : Controller
    {
        private TCMContext db = new TCMContext();        

        //
        // GET: /Exam/

        public ActionResult Index()
        {
            var exa = db.Exams.ToList();
            //foreach (var item in exa)
            //{
            //    if (item.Type != null)
            //    {
            //        var typename =
            //    db.MAPSTRINGBASEs
            //        .Where<MAPSTRINGBASE>(x => x.MAPSTRINGID.Equals(new Guid( item.Type)))
            //        .FirstOrDefault()
            //        .DISPLAYNAME;
            //        item.TYPEName = typename;
            //    }               
            //}
            return View(db.Exams.ToList ());
        }

        //
        // GET: /Exam/Details/5

        public ActionResult Details(Guid? id = null )
        {
            Exam Exam = db.Exams.Find(id);
            if (Exam == null)
            {
                return HttpNotFound();
            }
            return View(Exam);
        }

        //
        // GET: /Exam/Create

        public ActionResult Create()
        {
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable()
                , "StatusID", "StatusName");

            ViewBag.Type = new SelectList(db.ExamTypes.ToList().AsEnumerable(), "ExamTypeID", "ExamTypeName" );

           
            return View();
        }

        //
        // POST: /Exam/Create

        [HttpPost]
        public ActionResult Create(Exam exam)
        {
            if (ModelState.IsValid)
            {
                exam.ExamID = Guid.NewGuid();
                db.Exams.Add(exam);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable()
                 , "StatusID", "StatusName", exam.StatusID);

            ViewBag.Type = new SelectList(db.ExamTypes.ToList().AsEnumerable(), "ExamTypeID", "ExamTypeName", exam.ExamTypeID);


            return View(exam);
        }

        //
        // GET: /Exam/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            Exam Exam = db.Exams.Find(id);
            if (Exam == null)
            {
                return HttpNotFound();
            }

            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable()
                , "StatusID", "StatusName", Exam.StatusID);

            ViewBag.Type = new SelectList(db.ExamTypes.ToList().AsEnumerable(), "ExamTypeID", "StatusName", Exam.ExamTypeID);

           
            return View(Exam);
        }

        //
        // POST: /Exam/Edit/5

        [HttpPost]
        public ActionResult Edit(Exam Exam)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Exam).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable()
                , "StatusID", "StatusName", Exam.StatusID);

            ViewBag.Type = new SelectList(db.ExamTypes.ToList().AsEnumerable(), "ExamTypeID", "StatusName", Exam.ExamTypeID);

            

            return View(Exam);
        }

        //
        // GET: /Exam/Delete/5

        public ActionResult Delete(Guid? id = null  )
        {
            Exam Exam = db.Exams.Find(id);
            if (Exam == null)
            {
                return HttpNotFound();
            }
            //ViewBag.StatusID = new SelectList(db.MAPSTRINGBASEs
            //    .Where<MAPSTRINGBASE>
            //    (x => x.ATTRIBUTENAME.Equals("statusid") &&
            //        x.ENTITYNAME.Equals("Exam")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", Exam.StatusID);
            
           //var st = from MAPSTRINGBASE  MapStr in MAPSTRINGBASE  select MapStr.DISPLAYNAME ;
           // Guid g = Guid.Parse("803caef7-209e-44c4-b99d-4cb2f6768f03");
            

//var result = (from p in result
//              where (p.Prescription.Patient.last_name.Contains(foo) && 
//                     p.Prescription.Patient.first_name.Contains(foo2))
//              select p).Count(); 

           //ViewData["StatusID"] = db.RecordStates
           //    .Where(x => x.s == Exam.StatusID).Select(x => x.DISPLAYNAME);

            return View(Exam);
        }

        //
        // POST: /Exam/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Exam Exam = db.Exams.Find(id);
            db.Exams.Remove(Exam);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}